#!/bin/python3
import subprocess
import logging
from datetime import datetime
from dingtalkchatbot.chatbot import DingtalkChatbot

MSG_HEAD = 'nginx日志:\n'
WEBHOOK = 'https://oapi.dingtalk.com/robot/send?access_token=.....'
SECRET = '.....'
NGINX_LOG_PATH = '/usr/local/nginx/logs/*.log'
logging.basicConfig(
    filename="info.log",
    format="%(asctime)s-%(name)s-%(levelname)s %(message)s",
    level=logging.DEBUG)


def exec_command(shell):
    logging.info("exec shell: {0}".format(shell))
    result = subprocess.Popen(
        shell, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
        stdout=subprocess.PIPE, universal_newlines=True, shell=True, bufsize=1)
    stdout_list = result.stdout.readlines()
    stderr_list = result.stderr.readlines()
    _, _ = result.communicate()
    returncode = result.returncode
    if returncode != 0:
        logging.error("rxec shell eturncode: {0}".format(returncode))
        for stderr in stderr_list:
            logging.error(stderr)
        exit(1)
    logging.info("exec shell returncode: {0}".format(returncode))
    logging.info("exec shell is success")
    return stdout_list


def dingding_send(msg):
    logging.info("msg sed {0}".format(WEBHOOK))
    xiaoding = DingtalkChatbot(WEBHOOK, secret=SECRET)
    xiaoding.send_text(msg=msg)


if __name__ == '__main__':
    now = datetime.now()
    today = now.strftime('%d\\/%b\\/%Y')
    awk_shell = "awk 'BEGIN{req_time=0;req_max=0}/" + today \
                + """/{ s[$9]++;a++;b=b+$10;z[$4]++;
                status=$9 ~/^(2|3)/;
                if( status == 0 ){split($7,array,"?");r[array[1]"___"$9]++} }
                END{print("http状态 请求量 百分比");
                for(i in s) print i,s[i],s[i]/a*100"%";
                for(i in z) if (z[i]>max){req_time=i;max=z[i]};
                for(i in r)print i,r[i];
                print "总请求数: "a;
                print "返回流量: "b/1024/1024"MB";
                print "时间: "req_time,"qps: "z[req_time] }' """ + NGINX_LOG_PATH
    print(awk_shell)

    awk_stdout_list = exec_command(awk_shell)
    awk_msg_head = awk_stdout_list[0:1]
    awk_msg_body = awk_stdout_list[1:-3]
    awk_msg_tail = awk_stdout_list[-3:]
    awk_msg_body_fg = 0
    for k in range(awk_msg_body.__len__()):
        if '/' in awk_msg_body[k]:
            awk_msg_body_fg = k
            break
    awk_msg_http_status = awk_msg_body[1:awk_msg_body_fg]
    awk_msg_http_url = awk_msg_body[awk_msg_body_fg:-3]
    awk_msg_tail[-1] = awk_msg_tail[-1].replace('[', '')
    logging.info('开始排序')
    awk_msg_http_status = sorted(
        awk_msg_http_status,
        key=lambda l: int(l.split(' ')[1]),
        reverse=True)
    awk_msg_http_url = sorted(
        awk_msg_http_url,
        key=lambda l: int(l.split(' ')[1]),
        reverse=True
    )
    logging.info('结束排序')
    awk_msg_list = awk_msg_head + awk_msg_http_status + awk_msg_tail + awk_msg_http_url[0:10]
    awk_msgs = MSG_HEAD + ''.join([i for i in awk_msg_list])
    dingding_send(awk_msgs)